<?php
namespace app\common\service;

use app\common\logic\CartLogic;
use app\common\logic\GoodsLogic;
use think\facade\Db;

class Goods extends Base {
    static $AUTH_CHECK = false; //默认必须登录
    //获取商品详情
    public function getdetail(){
        $goods_id = input("id");

        $goodsLogic = new GoodsLogic();
        $listdata= ['map'=>['goods_id'=>['in',$goods_id]]];
        $goods = $goodsLogic->getListNew($listdata,true);
        if(empty($goods) || ($goods['is_on_sale'] == 0)){
            $result = [
                'status' => false,
                'msg' => '此商品不存在或者已下架',
                'data' => [],
            ];
            return $result;
        }
        $goods['goods_content'] = htmlspecialchars_decode($goods['goods_content']);
        $goods['leixing_name'] = Db::name('goods_leixing')->where('id',$goods['leixing_id'])->value('name');
        // 商品 图册
        $goods_images_list = explode(',',$goods['pics']);

        //查询默认地址
        $address = Db::name('user_address')->where(array( 'user_id' => $this->user_id))->order('is_default desc')->find();
        if($address){
            $area_arr = Db::name('region')->column('name','id');
            $address['province_name'] = $area_arr[$address['province']]??'';
            $address['city_name'] = $area_arr[$address['city']]??'';
            $address['district_name'] = $area_arr[$address['district']]??'';
            $address['town_name'] = $area_arr[$address['town']]??'';
        }
        $domain = getCurUrl();
        if(strpos($goods['original_img'],'http')===false){
            $goods['original_img'] = $domain.$goods['original_img'];
        }
        foreach($goods_images_list as $k=>$v){
            if(strpos($v,'http')===false){
                $goods_images_list[$k] = $domain.$v;
            }
        }
        $goods['pics'] = $goods_images_list;

        //商品评价
        $commentlist = Db::name('comment')
            ->alias('c')
            ->join('users u','c.user_id=u.user_id')
            ->where('goods_id',$goods_id)->where('is_show',1)->field('c.*,u.nickname,u.username,u.head_pic')->limit(3)->select();
        $commentcount = Db::name('comment')
            ->alias('c')
            ->join('users u','c.user_id=u.user_id')
            ->where('goods_id',$goods_id)->where('is_show',1)->count();
        if($commentlist){
            $domain_url = getCurUrl();
            foreach($commentlist as $ck=>$cv){
                $cv['img'] = explode(',',$cv['img']);
                $cv['head_pic'] = $cv['head_pic']?:$domain_url.'/static/img/touxiang.png';
                $cv['nickname'] = $cv['nickname']?:$cv['username'];
                $cv['add_time'] = date('Y-m-d H:i:s',$cv['add_time']);
                $commentlist[$ck] = $cv;
            }
        }
        $isfavorite = Db::name('goods_collect')->where("user_id", $this->user_id)->where("goods_id", $goods_id)->find();
        if($goods['sid']){
            $business = Db::name('suppliers')->where('id',$goods['sid'])->find();
        }
        if(!$goods['sid'] || !$business){
            $business = [
                'name' => tpCache('basic.store_name'),
                'logo' => tpCache('basic.store_logo'),
                'mobile' => tpCache('basic.kf_phone'),
            ];
        }
        $user_id = $this->user_id;
        if($user_id){
            $rs = Db::name('user_history')->where('user_id',$user_id)->where('goods_id',$goods_id)->where('type','shop')->find();
            if($rs){
                Db::name('user_history')->where('id',$rs['id'])->update(['createtime'=>time()]);
            }else{
                Db::name('user_history')->insert(['user_id'=>$user_id,'goods_id'=>$goods_id,'type'=>'shop','createtime'=>time()]);
            }
        }

        $shopset = self::$shop_set;
        $result = [
            'status' => true,
            'msg' => '',
            'data' => [
                'info' => $goods,
                'goods_images_list' => $goods_images_list,
                'address' => $address,
                'business' => $business,
                'commentlist' => $commentlist?:[],
                'commentcount' => $commentcount,
                'shopset' => $shopset,
                'isfavorite' => $isfavorite?1:0
            ],
        ];
        return $result;
    }
    //获取规格列表
    public function getguige(){
        $id = input('id');
        $product = Db::name('goods')->where('goods_id',$id)->find();
        $guige_data = json_decode($product['spec_items'],true);
        $ggselected = [];
        if($guige_data){
            foreach($guige_data as $v) {
                $ggselected[] = 0;
            }
        }

        $guige_lists = Db::name('spec_goods_price')->where('goods_id',$id)->column('*','key');

        $result = [
            'status' => true,
            'msg' => '',
            'data' => [
                'product' => $product,
                'guige_lists' => $guige_lists,
                'guige_data' => $guige_data,
                'ggselected' => $ggselected,
            ],
        ];
        return $result;
    }
    //收藏商品
    public function add_collect(){
        $goods_id = input('goods_id');
        $goodsLogic = new GoodsLogic();
        $result = $goodsLogic->collect_goods($this->user_id,$goods_id);
        return $result;
    }
    //获取收藏列表
    public function collect_lists(){
        $user_id = $this->user_id;
        if(!$user_id){
            //强制登录
            $result = [
                'status'=>false,
                'msg' => '请登录',
                'data' =>[
                    'need_login' =>1
                ]
            ];
            return $result;
        }
        $page = input('page');
        //列表过滤器，生成查询Map对象
        $map = [];
        $map['c.user_id'] = $this->user_id;

        //查询订单列表
        $field = 'c.*,g.goods_id,g.goods_name,g.leixing_id,g.original_img,g.shop_price,g.market_price';
        $lists = Db::name('goods_collect')
            ->alias('c')
            ->join('goods g','c.goods_id=g.goods_id')
            ->where($map)
            ->order('c.id desc')
            ->field($field)
            ->paginate(15,false,['page'=>$page,'list_rows'=>15]);
        if($lists->total()){
            foreach($lists as $k=>$v){
                $v['w_time'] = date('Y-m-d H:i:s',$v['add_time']);
                $lists->offsetSet($k,$v);
            }
        }
        $result = [
            'status' => true,
            'msg' => '',
            'data' => $lists->items()
        ];
        return $result;
    }

    //获取评价列表
    public function comment_lists(){
        $page = input('pagenum');
        $goods_id = input('goods_id');
        //列表过滤器，生成查询Map对象
        $map = [];
//        $map['c.user_id'] = $this->user_id;
        $map['c.goods_id'] = $goods_id;
        $map['c.is_show'] = 1;
        //查询订单列表
        $field = 'c.*,u.nickname,u.head_pic';
        $lists = Db::name('comment')
            ->alias('c')
            ->join('users u','u.user_id=c.user_id')
            ->where($map)
            ->order('c.comment_id desc')
            ->field($field)
            ->paginate(15,false,['page'=>$page,'list_rows'=>15]);
        if($lists->total()){
            $domain_url = getCurUrl();
            foreach($lists as $k=>$v){
                $v['img'] = explode(',',$v['img']);
                $v['head_pic'] = $v['head_pic']?:$domain_url.'/static/img/touxiang.png';
                $v['nickname'] = $v['nickname']?:$v['username'];
                $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
                $lists->offsetSet($k,$v);
            }
        }
        $result = [
            'status' => true,
            'msg' => '',
            'data' => $lists->items()
        ];
        return $result;
    }

    //获取分类列表
    public function cat_lists(){
        $page = input('page');
        $goods_id = input('goods_id');
        //列表过滤器，生成查询Map对象
        $map = [];
        $map['parent_id'] = 0;
        $map['is_show'] = 1;
       // $map['c.user_id'] = $this->user_id;
       // $map['c.goods_id'] = $goods_id;
        //查询订单列表
        $field = 'c.*';
        $lists = Db::name('goods_category')
            ->alias('c')
            ->where($map)
            ->order('sort desc,c.id desc')
            ->field($field)
            ->paginate(15,false,['page'=>$page,'list_rows'=>15]);
        if($lists->total()){
            foreach($lists as $k=>$v){
                $v['child'] = Db::name('goods_category')->where(['parent_id'=>$v['id'],'is_show'=>1])->select();
                $lists->offsetSet($k,$v);
            }
        }
        $result = [
            'status' => true,
            'msg' => '',
            'data' => $lists->items()
        ];
        return $result;
    }

}
